import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter_app/models/room_list_item_data.dart';
import 'package:flutter_app/pages/search/fliter_bar/data2.dart';
import 'package:flutter_app/pages/search/fliter_bar/filter_bar.dart';
import 'package:flutter_app/pages/search/search_item.dart';
import 'package:flutter_app/utils/dio_http.dart';
import 'package:flutter_app/utils/scoped_model_helper.dart';
import 'package:flutter_app/widgets/search_bar.dart';
import 'fliter_bar/fliter_drawer_bar.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';

class SearchPage extends StatefulWidget {
  const SearchPage({Key? key}) : super(key: key);

  @override
  _SearchPageState createState() => _SearchPageState();
}

class _SearchPageState extends State<SearchPage> {
  final int _page = 1; //加载的页数
  final int _pageSize = 10; //加载的页量
  bool isLoading = false; //是否正在加载数据
  List<RoomListItemData> list = [];
  _onFilterBarChange(FilterBarResult data) async {
    var cityId = Uri.encodeQueryComponent(ScopedHelperModel.getAreaId(context));
    var area = Uri.encodeQueryComponent(data.areaId);
    var mode = Uri.encodeQueryComponent(data.rentTypeId);
    var price = Uri.encodeQueryComponent(data.priceId);
    var more = Uri.encodeQueryComponent(data.moreIds.join(','));
    String url =
        '/houses?cityId=$cityId&area=$area&rentType=true&price=null&start=$_page&end=$_pageSize';
    var res = await DioHttp.of(context).get(url);
    var resString = json.decode(res.toString());
    List dataMap = resString["body"]["list"];
    List<RoomListItemData> resList =
        dataMap.map((e) => RoomListItemData.fromJson(e)).toList();
    setState(() {
      list = resList;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      endDrawer: const FilterDrawer(),
      appBar: AppBar(
        actions: [Container()],
        elevation: 0.0,
        backgroundColor: Colors.white,
        title: SearchBar(
          showLocation: true,
          showMap: true,
          onTabSearch: () {},
        ),
      ),
      body: Column(
        children: [
          FilterBar(
            onChange: (v) {
              _onFilterBarChange(v);
              // setState(() {
              //   params = v;
              // });
            },
          ),
          Expanded(
            child: ListView(
              children: list.map((e) => HouseItem(e)).toList(),
            ),
          )
        ],
      ),
    );
  }
}
